package service;

import java.io.File;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class ScanDirectoryJob implements Job {
	static Log logger = LogFactory.getLog(ScanDirectoryJob.class);

	public void execute(JobExecutionContext context) throws JobExecutionException {
		JobDetail jobDetail = context.getJobDetail();
		String jobName = jobDetail.getKey().getName();
		logger.info(jobName + " fired at " + new Date());
		JobDataMap dataMap = jobDetail.getJobDataMap();
		String dirName = dataMap.getString("SCAN_DIR");
		logger.info("dirName : " + dirName);
		if (dirName == null) {
			throw new JobExecutionException("Directory not configured");
		}
		File dir = new File(dirName);
		if (!dir.exists()) {
			throw new JobExecutionException("Invalid Dir " + dirName);
		}
		File[] files = dir.listFiles();
		for (File file : files) {
			if (!file.getName().contains(".xml")) {
				continue;
			}
			File aFile = file.getAbsoluteFile();
			long fileSize = file.length();
			String msg = aFile + " - Size: " + fileSize;
			logger.info(msg);
		}
	}

}
